package com.tzy.evaluation.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzy.common.constant.CacheConstant;
import com.tzy.common.constant.Result;
import com.tzy.common.constant.WebResult;
import com.tzy.common.constant.WebResultMsgCode;
import com.tzy.common.controller.BaseController;
import com.tzy.common.entity.Evaluation;
import com.tzy.common.utils.UpPhotoNameUtils;
import com.tzy.evaluation.service.EvaluationService;
import com.tzy.evaluation.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户评价表 前端控制器
 * </p>
 *
 * @author 谭同学
 * @since 2022-03-02
 */
@Slf4j
@RestController
@RequestMapping("/evaluation")
public class EvaluationController extends BaseController {
    @Value("${file.upload.url}")
    private String uploadUrl;//文件上传路径
    @Value("${file.upload.prefix-url}")
    private String prefixUrl;//服务器路径前缀
    @Resource
    private RedisService redisService;
    @Resource
    private EvaluationService evaluationService;
    @GetMapping("/pending/evaluation/list/userId")
    public Result getWaitingForEvaluationByUserId(HttpServletRequest request){
        int userId = getUserId(request);
        List<Map<String, Object>> mapList = evaluationService.getPendingEvaluationListByUserId(userId);
        return Result.success(mapList);
    }

    @GetMapping("/url/userId")
    public WebResult getUrl(HttpServletRequest request){
        int userId = getUserId(request);
        String videoOrImageUrlOfTheReview = redisService.getString("videoOrImageUrlOfTheReview" + userId);
        log.info("redis videoOrImageUrlOfTheReview="+videoOrImageUrlOfTheReview);
        return new WebResult(WebResultMsgCode.SUCCESS,videoOrImageUrlOfTheReview);
    }

    @GetMapping("/page/list/userId")
    public WebResult getEvaluationListByUserId(HttpServletRequest request,@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer size){
        int userId = getUserId(request);
        log.info("userId="+userId);
//        redisService.getString(CacheConstant.USERNAME_PREFIX+user.getId())
        Page<Evaluation> page = evaluationService.getEvaluationListByUserId(userId,pageNum, size);
        return new WebResult(WebResultMsgCode.SUCCESS,page);
    }
    @GetMapping("/page/{dishesId}")
    public WebResult getEvaluationPageByDishesId(@PathVariable int dishesId,@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer size){
        Page<Evaluation> page = evaluationService.getEvaluationPageByDishesId(dishesId,pageNum, size);
        return new WebResult(WebResultMsgCode.SUCCESS,page);
    }
    @GetMapping("/page")
    public WebResult getEvaluationByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer size){
        Page<Evaluation> page = evaluationService.getEvaluationByPage(pageNum, size);
        return new WebResult(WebResultMsgCode.SUCCESS,page);
    }
    @PostMapping("/insert")
    public WebResult save(HttpServletRequest request,String evaluationDescribe, int evaluationScore ){
        int userId = getUserId(request);
        log.info("userId={},evaluationDescribe={},evaluationScore={}",userId,evaluationDescribe,evaluationScore);
        String videoOrImageUrlOfTheReview = null;
        String redisServiceString = redisService.getString(CacheConstant.HEADER_EVALUATION_ID + userId);
        if(null != redisServiceString){
            videoOrImageUrlOfTheReview = redisServiceString;
        }
        Evaluation evaluation = new Evaluation();
        evaluation.setUserId(userId);
        evaluation.setEvaluationDescribe(evaluationDescribe);
        evaluation.setEvaluationScore(evaluationScore);
        evaluation.setVideoOrImageUrlOfTheReview(videoOrImageUrlOfTheReview);
        boolean save = evaluationService.save(evaluation);
        return new WebResult(WebResultMsgCode.SUCCESS,save);
    }
    @DeleteMapping("/delete")
    public WebResult remove(int id){
        log.info("id="+ id);
        Evaluation evaluation = new Evaluation();
        evaluation.setId(id);
        evaluation.setEvaluationStatus(1);
        boolean updateById = evaluationService.updateById(evaluation);
        return new WebResult(WebResultMsgCode.SUCCESS,updateById);
    }

    @GetMapping("/info/{id}")
    public Evaluation getInfo(@PathVariable  int id){
        Evaluation evaluation = evaluationService.getById(id);
        return evaluation;
    }
}
